<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>权限列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区域 -->
        <el-card>
            <!-- 添加用户按钮 -->
            <el-col :span="5">
            <el-button type="primary" @click="visibleUser=true">添加用户</el-button>
            </el-col>
            <!-- 角色表格区域 -->
            <el-table :data="rolesLise" border stripe>
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-row  v-for="(item1,i1) in scope.row.children" :class="[i1===0?'bdtop':'','itemAligin']" :key="item1.id" >
                      <!-- 一级权限渲染 -->
                      <el-col :span="6">
                        <el-tag closable >
                          {{item1.authName}}
                        </el-tag>
                        <i class="el-icon-caret-right"></i>
                      </el-col>
                      <!-- 二级权限渲染 -->
                       <el-col :span="18"  >
                           <el-row v-for="item2 in item1.children" :key="item2.id" class='itemAligin'>
                              <el-col :span="8" >
                                  <el-tag type="info" closable>{{item2.authName}}</el-tag>
                              </el-col>
                              <el-col :span="16" >
                                  <el-tag  v-for="item3 in item2.children" closable :key="item3.id" type="warning" @close="removeRolesTag(scope.row,item3.id)">{{item3.authName}}</el-tag>
                              </el-col>
                          </el-row>
                      </el-col>
                    </el-row>
                  </template>
                </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="角色操作" prop="rolename">
                    <template  slot-scope="scope" >
                        <el-button size="small" type="primary" icon="el-icon-edit">编辑</el-button>
                        <el-button size="small" type="success" icon="el-icon-delete">删除</el-button>
                        <el-button size="small" type="warning" icon="el-icon-upload" @click="setUserPowersDialog(scope.row,scope.row.id)" >分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
            </el-card>
            <!-- 分配权限对话框 -->
            <el-dialog
              title="分配权限"
              :visible.sync="showUserPowersDialog"
              width="30%"
              @close="closeRolesDialog"
             >
              <el-tree :data="showUserRolestree" :props="treeProps"
               default-expand-all
               ref="rolesDialog"
              check-on-click-node
              show-checkbox
              node-key="id"
              :default-checked-keys="checkTreeArray">
              </el-tree>
              <span slot="footer" class="dialog-footer">
                <el-button @click="showUserPowersDialog = false">取消</el-button>
                <el-button type="primary" @click="defindDialogbtn()">确 定</el-button>
              </span>
            </el-dialog>
    </div>
</template>

<script>
export default {
  data () {
    return {
      rolesLise: [],
      // 打开分配权限对话框
      showUserPowersDialog: false,
      // 获取分配权限用户列表
      showUserRolestree: [],
      // 树形对象的属性绑定对象
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中的权限树选项数组id
      checkTreeArray: [],
      // 点击的角色id
      rolesId: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return
      this.rolesLise = res.data
    },
    // 移除用户权限
    async removeRolesTag (role, rightId) {
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') {
        return this.$Message.info('取消了删除')
      }
      this.$Message.success('删除权限成功')
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
      console.log(res)
      if (res.meta.status !== 200) return this.$$Message.error('删除失败')
      role.children = res.data
    },
    // 通过递归获取所有三级节点的id
    getThreeId (roles, array) {
      if (!roles.children) {
        return array.push(roles.id)
      }
      roles.children.forEach(item => {
        this.getThreeId(item, array)
      })
    },
    // 监听关闭分配权限对话框
    closeRolesDialog () {
      this.checkTreeArray = []
    },
    // 监听点击事件分配权限对话框
    async setUserPowersDialog (roles, rolesId) {
      this.rolesId = rolesId
      // 获取三级节点
      this.getThreeId(roles, this.checkTreeArray)

      this.showUserPowersDialog = true
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$Message.Error('获取用户权限失败')
      }
      this.showUserRolestree = res.data
    },
    // 监听对话框确定按钮
    async defindDialogbtn () {
      const key = [
        ...this.$refs.rolesDialog.getCheckedKeys(),
        ...this.$refs.rolesDialog.getHalfCheckedKeys()

      ]

      const idStr = key.join(',')
      console.log(this.rolesId, idStr)
      const { data: res } = await this.$http.post(`roles/${this.rolesId}/rights`, { rids: idStr })
      if (res.meta.status !== 200) {
        return this.$Message.error('修改失败')
      }
      this.$Message.success('分配成功')
      this.getRolesList()
      this.showUserPowersDialog = false
    }

  }
}
</script>

<style lang="less" scoped>
.el-row{
  border-bottom: 1px solid #cccc;
  margin: 10px;
  padding: 10px;
  .el-tag{
    margin: 10px;
  }
}
.bdtop{
  border-top: 1px solid #cccc;
}
.itemAligin{
  display: flex;
  align-items: center;
}
</style>
